package Test;

/**
 * @author shy_black
 * @date 2019/3/15 14:55
 * @Description:
 */
public class 排序练习 {
    public static void main(String[] args) {
        int[] arr = new int[]{5, 4, 7, 3, 7, 3, 7, 2, 67, 3};
        //bubbleSort(arr);
        //selectSort(arr);
        //insertSort(arr);
        mergeSort(arr);
    }

    public static void mergeSort(int[] arr) {
        if (arr.length <= 1)
            return;
        int l = 0;
        int r = arr.length - 1;
        int mid = (l + r) / 2;
        mergeInternal(arr, l, r);
        for(int i : arr) {
            System.out.print(i+ "-");
        }
    }

    public static void mergeInternal(int[] arr, int l, int r) {
        if (l >= r)
            return;
        int mid = (l+r)/2;
        mergeInternal(arr, l, mid);
        mergeInternal(arr, mid + 1, r);
        merge(arr, l, mid, r);
    }

    public static void merge(int[] arr, int l, int mid, int r) {
        int[] temp = new int[arr.length];
        //p1记录第一个数组的元素位置
        //p2记录第二个数组的元素位置
        //k用来记录temp数组的元素位置
        int p1 = l;
        int k = l;
        int p2 = mid + 1;
        //当俩个数组都不越界时，比较看那个数组小，则放到temp数组中
        while (p1 <= mid && p2 <= r) {
            if (arr[p1] <= arr[p2])
                temp[k++] = arr[p1++];
            else
                temp[k++] = arr[p2++];
        }
        //那个数组中的元素还有剩余，则放入temp数组中
        while (p1 <= mid) temp[k++] = arr[p1++];
        while (p2 <= r) temp[k++] = arr[p2++];
        //将temp数组赋值给arr数组
        for (int i = l; i <= r; i++)
            arr[i] = temp[i];
    }


    public static void selectSort(int[] arr) {
        if (arr.length <= 1)
            return;
        for (int i = 0; i < arr.length - 1; i++) {
            int temp = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[i]) {
                    int t = arr[i];
                    arr[i] = arr[j];
                    arr[j] = t;
                }
            }
        }
        for (int i : arr) {
            System.out.print(i + "-");
        }
    }

    public static void insertSort(int[] arr) {
        if (arr.length <= 1)
            return;
        int j;
        int temp;
        for (int i = 0; i < arr.length; i++) {
            temp = arr[i];

            for (j = i - 1; j >= 0 && arr[j] > temp; j--) {
                arr[j + 1] = arr[j];
            }
            int t = temp;
            temp = arr[j + 1];
            arr[j + 1] = t;
        }
        for (int i : arr) {
            System.out.print(i + "-");
        }

    }


    public static void bubbleSort(int[] arr) {
        if (arr.length <= 1)
            return;
        for (int i = 0; i < arr.length; i++) {
            boolean flag = true;
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = false;
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
            if (flag == true)
                break;
        }
        for (int i : arr) {
            System.out.print(i + "-");
        }
    }
}
